package frank.zou.study.jmock.customized.action;

import java.util.Arrays;
import java.util.Collection;

import org.hamcrest.Description;
import org.jmock.api.Action;
import org.jmock.api.Invocation;

public class AddElementsAction<T> implements Action {
	
	private Collection<T> elements; 
	
	public AddElementsAction(Collection<T> elements) { 
		this.elements = elements; 
	}

	public void describeTo(Description description) {
		description.appendText("adds ")
		.appendValueList("", ",", "", elements)
		.appendText(" to a collection");
	}

	public Object invoke(Invocation invocation) throws Throwable {
		((Collection<T>)invocation.getParameter(0)).addAll(elements);
		return null;
	}
	
	public static <T> Action addElements(T...newElements) { 
		return new AddElementsAction(Arrays.asList(newElements));
	}

}
